﻿using System;
using System.Collections.Generic;
 
using System.Text;
using GGSharp.Packets;

namespace GGSharp
{
    static public class GGPacketFactory
    {
        static private Dictionary<int, Type> _packets = new Dictionary<int, Type>();

        static GGPacketFactory()
        {
            Add(PacketType.GG_ADD_NOTIFY, typeof(Packets.AddNotifyPacket));
            Add(PacketType.GG_PONG, typeof(Packets.PongPacket));
            Add(PacketType.GG_WELCOME, typeof(Packets.WelcomePacket));
            Add(PacketType.GG_LOGIN_OK, typeof(Packets.LoginOkPacket));
            Add(PacketType.GG_RECV_MSG, typeof(Packets.ReceivedMessagePacket));
            Add(PacketType.GG_SEND_MSG_ACK, typeof(Packets.MessageAcknowledgePacket));
            Add(PacketType.GG_DISCONNECTING, typeof(Packets.DisconnectingPacket));
            Add(PacketType.GG_DISCONNECTING2, typeof(Packets.DisconnectingConfirmationPacket));
            Add(PacketType.GG_STATUS77, typeof(Packets.Status77Packet));
            Add(PacketType.GG_STATUS80, typeof(Packets.Status77Packet));
            Add(PacketType.GG_NOTIFY_REPLY77, typeof(Packets.NotifyReply77Packet));
            Add(PacketType.GG_NOTIFY_REPLY80, typeof(Packets.NotifyReply77Packet));
            Add(PacketType.GG_XML_EVENT, typeof(Packets.XMLEventPacket));
            Add(PacketType.GG_USERLIST_REPLY, typeof(Packets.UserListReplyPacket));
            Add(PacketType.GG_PUBDIR50_REPLY, typeof(Packets.PubDirResponse50Packet));
            Add(PacketType.GG_LOGIN_FAILED, typeof(Packets.LoginFailedPacket));
            Add(PacketType.GG_NEED_EMAIL, typeof(Packets.NeedEmailPacket));
        }

        public static void Add(int type, Type packetType)
        {
            _packets[type] = packetType;
        }

        public static void Add(PacketType type, Type packetType)
        {
            Add((int)type, packetType);
        }

        public static HeaderPacket Get(int type)
        {
            Type packetType;
            if (_packets.ContainsKey(type))
            {
                packetType = _packets[type];
                return (HeaderPacket)packetType.GetConstructor(new Type[] { }).Invoke(null);
            }
            else
            {
                return new GenericPacket(type);
            }
        }
    }
}
